package reading.web.controllers;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reading.common.pojos.EntityResponse;
import reading.data.DataUtils;
import reading.data.model.Article;
import reading.mybatis.Page;
import reading.mybatis.Pageable;
import reading.service.ArticleService;

import static reading.common.pojos.EntityResponse.success;

/**
 * ${DESCRIPTION}
 *
 * @author txw
 * @since 2019-02-15 12:46
 **/
@RestController
@RequestMapping("/inapi/article")
public class ArticleController {
    private final ArticleService articleService;
    @Autowired
    public ArticleController(ArticleService articleService) {
        this.articleService = articleService;
    }
    @GetMapping("findPage")
    @ApiOperation("分页查询书籍 信息 ")
    public EntityResponse<Page<Article>> findPage(@RequestParam(value = "page",
            required = false, defaultValue = Pageable.STR_PAGE_INDEX) Integer page,
                                                  @RequestParam(value = "size", required = false, defaultValue = Pageable.STR_PAGE_SIZE) Integer size,
                                                  @RequestParam(value =
                                                          "orderField",
                                                          required = false,
                                                          defaultValue =
                                                                  "sort") String orderField,
                                                  @RequestParam(value =
                                                          "direction",
                                                          required = false,
                                                          defaultValue =
                                                                  "asc") String direction,
                                                  @RequestParam Long bookId){
        Pageable pageable = DataUtils.pageable(page, size, orderField, direction);
        pageable.addParam("bookId",bookId);
        return success(articleService.findPage(pageable));
    }
}
